package com.tutos.ui.monitor;

import javax.inject.Inject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.tutos.dao.MonitorDAO;
import com.tutos.model.Monitor;
import com.tutos.ui.TutosApplication;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class EditMonitor {
	
	private VerticalLayout layout = new VerticalLayout();
	private MonitorDAO monitorDAO;
   
    @Inject
    public EditMonitor(MonitorDAO monitorDAO) {
    
    	this.monitorDAO=monitorDAO;    	    	
    	buildLayout();
    
    }	    
    void buildLayout(){
    	    	
    	layout.setSpacing(true);    	
    	final Button saveButton = new Button("Save");        

        
       Subject currentUser = SecurityUtils.getSubject();		
       Monitor monitorDate=monitorDAO.getMonitorByCorreo((String) currentUser.getSession().getAttribute("username"));       
       BeanItem<Monitor> monitorItem = new BeanItem<Monitor>(monitorDate);
       Form monitorForm = new MonitorEditForm(monitorItem);
      
       layout.addComponent(monitorForm);
       layout.setComponentAlignment(monitorForm, Alignment.MIDDLE_CENTER);   	
       layout.addComponent(saveButton);
   	   layout.setComponentAlignment(saveButton, Alignment.BOTTOM_RIGHT);
   	   
   	   final Monitor monitorEdit = monitorItem.getBean();   		   	
   	   
   	   saveButton.addListener(new ClickListener() {
			
			private static final long serialVersionUID = 1L;
		
			@Override
			public void buttonClick(ClickEvent event) {
				Window mainWindow = TutosApplication.getInstance().getMainWindow();							
				
				monitorDAO.updateEntity(monitorEdit);	
				mainWindow.showNotification("Actualizacion Exitosa", Notification.TYPE_HUMANIZED_MESSAGE);											
				
			}
		});
    }
    
	public Component getComponent() {
		return layout;
	}	
	
}
